package com.ls;

import javafx.util.Pair;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 插旗法
 * @author : 梁苏
 * @date : 2023/2/5 19:57
 **/
public class DateQuery {

    Map<Integer, Integer> map = new HashMap<>();

    LocalDate now = LocalDate.now();
    long nowDay = now.toEpochDay();

    public static void main(String[] args) {
        // 2-5 3-10
        // 2+   3+  6-   11-
    }

    /**
     * 加入时间对
     *
     * @param dates 日期对
     */
    public void addTimes(List<Pair<LocalDate, LocalDate>> dates) {
        for (Pair<LocalDate, LocalDate> date : dates) {
            int start = (int) (date.getKey().toEpochDay() - nowDay);
            int end = (int) (date.getValue().toEpochDay() - nowDay);
            if (start < 0) {
                start = 0;
            }

            add(start, end);
        }
    }

    public void add(Integer start, Integer end){
        Integer count = map.get(start);
        if(count == null){
            map.put(start, 1);
        }else{
            map.put(start, count + 1);
        }

        Integer endCount = map.get(end+1);
        if(endCount == null){
            map.put(end + 1, -1);
        }else{
            map.put(end + 1, endCount - 1);
        }
    }

    public void print(){
        ArrayList<Integer> indexs = new ArrayList<>(map.keySet());
        Collections.sort(indexs);

        // 1: 2-3  2:3-5 1: 6-10 0:10-
        int cur = 0;
        int start = -1;
        for (Integer index : indexs) {
            Integer num = map.get(index);
            cur += num;

            if(start == -1){
                start = index;
                System.out.print(cur + ": [" + start);
            }else{
                System.out.println("," + (index-1) + "]");
                start = index;
                System.out.print(cur + ": [" + start);
            }

        }
        System.out.println();
    }

    /**
     * 获取能够使用的时间段
     *
     * @return 时间段
     */
    public List<QAdTime> getDisableDates() {
        ArrayList<Integer> indexs = new ArrayList<>(map.keySet());
        Collections.sort(indexs);

        List<QAdTime> times = new ArrayList<>();

        int cur = 0;
        QAdTime time = null;
        for (Integer index : indexs) {
            Integer num = map.get(index);
            cur += num;

            if(time == null){
                time = new QAdTime();
                time.setStart(Long.valueOf(index));
                System.out.print(cur + ": [" + index);

                if(cur >= 3){
                    times.add(time);
                }

            }else{
                time.setEnd((long) (index - 1));
                times.add(time);
                System.out.println("," + (index-1) + "]");

                time = new QAdTime();
                time.setStart(Long.valueOf(index));
                System.out.print(cur + ": [" + index);

                if(cur >= 3){
                    times.add(time);
                }
            }

        }
        System.out.println();

        return times;
    }

}


class QAdTime {
    /**
     * 开始时间
     */
    private Long start;

    /**
     * 结束时间
     */
    private Long end;

    public Long getStart() {
        return start;
    }

    public void setStart(Long start) {
        this.start = start;
    }

    public Long getEnd() {
        return end;
    }

    public void setEnd(Long end) {
        this.end = end;
    }

}